import {
  noticeJson,
  dealShareNoticeJson,
  deleteNotice
} from '@/services/notice';

export default {
  namespace:'notice',
  state: {
    total:0,
    items:[],
    loading:true
  },
  effects: {
    *getNotice({ payload, callback }, { call, put }) {
      const response = yield call(noticeJson, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *dealShareNotice({ payload, callback }, { call, put }) {
      const response = yield call(dealShareNoticeJson, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *delectNotice({
      payload,
      callback
    }, {
      call,
      put
    }) {
      const response = yield call(deleteNotice, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
